package com.module.app.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.View;

import com.module.app.pop.CommonPop;
import com.module.app.bean.IType;
import com.module.app.bean.IType;
import com.module.base.cache.CacheSDK;
import com.module.base.utils.ToastUtils;
import com.module.app.R;
import com.module.frame.app.AppManager;

/**
 * 引导评价
 */
public class EvaluateUtils {

    /**
     * 启动
     */
    public static void startUp() {
        CacheSDK.put(IType.ICache.START_UP, getStartUp() + 1);
    }

    private static int getStartUp() {
        int startUp = CacheSDK.get(IType.ICache.START_UP, Integer.class);
        return startUp;
    }

    /**
     * 获取拒绝评价次数
     */
    private static int getRejectEvaluate() {
        int reject = CacheSDK.get(IType.ICache.EVALUATE_REJECT, Integer.class);
        return reject;
    }

    /**
     * 绝评价次数
     */
    private static void setRejectEvaluate() {
        CacheSDK.put(IType.ICache.EVALUATE_REJECT, getRejectEvaluate() + 1);

    }

    /**
     * 是否评价过
     */
    private static boolean isHasEvaluate() {
        boolean isHasEvaluate = CacheSDK.get(IType.ICache.EVALUATE_HAS, Boolean.class);
        return isHasEvaluate;
    }

    /**
     * 评价
     */
    private static void evaluate() {
        CacheSDK.put(IType.ICache.EVALUATE_HAS, true);
    }

    private static Handler handler;

    /**
     * 引导评价
     */
    public static void onGuideEvaluate() {
        if (handler == null) {
            handler = new Handler();
        }
        if (isHasEvaluate() || handler == null) {
            return;
        }
        handler.removeCallbacksAndMessages(null);
        //10，30，50，70
        if (getStartUp() > (getRejectEvaluate() * 20) + 10) {
            //启动评价
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Activity activity = AppManager.getInstance().currentActivity();
                    if (activity == null || activity.isFinishing()) {
                        return;
                    }
                    new CommonPop.Builder(activity)
                            .setCanceledOnTouchOutside(false)
                            .setTitle("评价")
                            .setContent("如果您喜欢，占用您一点点时间给我们评价下吧，您的好评就是我们动力，我们会努力做好更好的产品。感谢您的支持！")
                            .setCancelButton(R.string.cancel)
                            .setConfirmButton(R.string.evaluate)
                            .setOnCancelListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    setRejectEvaluate();
                                }
                            })
                            .setOnConfirmListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    evaluate();
                                    ToastUtils.showShort("感谢您的支持！");
                                    try {
                                        Uri uri = Uri.parse("market://details?id=" + activity.getPackageName());
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        activity.startActivity(intent);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            })
                            .show();

                }
            }, 3000);
        }
    }

    public static void destroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

}
